/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  VersionHelper.java                                      
 *  File Contents Outline :  版本久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.dbobject.DbVersion;
import storage.util.StorageManager;
import cn.edu.thu.log.util.PrintClasspath;

public class VersionHelper {

    /**
     * 将version保存到LaUDMS数据库中
     *
     * @param version 要存储的模型对象
     */
    public void saveVersion(DbVersion version) {
    //    StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_version);
             PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_version"));
//        sm.open();
        sm.setRowKey(version.getRowKey());
        sm.put(DbVersion.cl_versionNum, version.getVersionNum());
        sm.put(DbVersion.cl_updateTime, version.getUpdateTime());
        sm.put(DbVersion.cl_validStartTime, version.getValidStartTime());
        sm.put(DbVersion.cl_validEndTime, version.getValidEndTime());
        ArrayList<String> logHeadList = version.getLogHeadList();
        for (int i = 0; i < logHeadList.size(); i++) {
            sm.put(DbVersion.cl_logHead + "_" + logHeadList.get(i), logHeadList.get(i));
        }
//        sm.close();

    }
       public void updateVersion(DbVersion version) {
             PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_version"));
//        sm.open();
        sm.delete(version.getRowKey());
        sm.setRowKey(version.getRowKey());
        sm.put(DbVersion.cl_versionNum, version.getVersionNum());
        sm.put(DbVersion.cl_updateTime, version.getUpdateTime());
        sm.put(DbVersion.cl_validStartTime, version.getValidStartTime());
        sm.put(DbVersion.cl_validEndTime, version.getValidEndTime());
        ArrayList<String> logHeadList = version.getLogHeadList();
        for (int i = 0; i < logHeadList.size(); i++) {
            sm.put(DbVersion.cl_logHead + "_" + logHeadList.get(i), logHeadList.get(i));
        }
//        sm.close();

    }

    /**
     * 将一系列logHead保存到LaUDMS数据库中
     *
     * @param logHeads 要存储的模型对象列表
     */
    public void saveVersions(List<DbVersion> versions) {
             PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_version"));
//        sm.open();
        for (int i = 0; i < versions.size(); i++) {
            DbVersion version = versions.get(i);
            sm.setRowKey(version.getRowKey());
            sm.put(DbVersion.cl_versionNum, version.getVersionNum());
            sm.put(DbVersion.cl_updateTime, version.getUpdateTime());
            sm.put(DbVersion.cl_validStartTime, version.getValidStartTime());
            sm.put(DbVersion.cl_validEndTime, version.getValidEndTime());
            ArrayList<String> logHeadList = version.getLogHeadList();
            for (int j = 0; j < logHeadList.size(); j++) {
                sm.put(DbVersion.cl_logHead + "_" + logHeadList.get(j), logHeadList.get(j));

            }
//          sm.close();
        }
    }

    public List<DbVersion> getVersions(final String columnName, final String value) {
        List<DbVersion> result = new LinkedList<DbVersion>();
             PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_version"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        
        if(crs  ==  null || crs.size()== 0 ){
            System.out.println("version value:"+value);
        }
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String versionNum = null, validStartTime = null, validEndTime = null, updateTime = null;
                ArrayList<String> logHeadList = new ArrayList();
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbVersion.cl_versionNum)) {
                        versionNum = columnsValue;
                    } else if (columnsName.equals(DbVersion.cl_validStartTime)) {
                        validStartTime = columnsValue;
                    } else if (columnsName.equals(DbVersion.cl_validEndTime)) {
                        validEndTime = columnsValue;
                    } else if (columnsName.equals(DbVersion.cl_updateTime)) {
                        updateTime = columnsValue;
                    } else if (columnsName.startsWith(DbVersion.cl_logHead)) {
                        logHeadList.add(columnsValue);
                    }
                }
                DbVersion pVersion = new DbVersion(rowKey, versionNum, validStartTime, validEndTime, updateTime, logHeadList);
                result.add(pVersion);
            }
        } catch (Exception ex) {
            Logger.getLogger(DbVersion.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public ArrayList<String> getAllVersionName() {
             PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_version"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        try
        {
            Thread.sleep(10000);
        }
        catch (Exception e ){
        }
        
        ArrayList<String> versionNumList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String versionNum = "";
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbVersion.cl_versionNum)) {
                        versionNum = columnsValue;
                    }
                }
                if (!versionNumList.contains(versionNum)&&versionNum.length()>0) {
                    versionNumList.add(versionNum);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(DbVersion.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return versionNumList;
    }

    public void deleteVersion(final String rowKey) {
              PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_version"));
//        sm.open();
        sm.delete(rowKey);
//        sm.close();
    }

    /**
     * 为产品添加日志体字段
     *
     * @param version 产品
     * @param logHead 日志体字段
     */
    public void addLogHead(DbVersion version, String logHead) {
             PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
         StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_version"));
//        sm.open();
        sm.setRowKey(version.getRowKey());
        sm.put(DbVersion.cl_logHead + "_" + logHead, logHead);
//        sm.close();
    }

//    /**
//     *
//     */
//    public void deleteLogHead(DbVersion version, String logHead) {
//        StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_version);
////        sm.open();
//        sm.setRowKey(version.getRowKey());
//        // sm.put(DbVersion.cl_logBody + "_" + logBody, logBody);
//        sm.delete(DbVersion.cl_logHead + "_" + logHead, logHead);
////        sm.close();
//
//    }

    public void deleteLogBody(String versionName, String logBodyname) {
        throw new UnsupportedOperationException("Not yet implemented");
    }
    public static void main(String [] args){
//        VersionHelper VersionH = new VersionHelper();
//        List<String> versions=VersionH.getAllVersionName();
//        for(String xxh : versions){
//            System.out.println(xxh);
//        }
//        System.out.println("size:"+versions.size());
    	PrintClasspath.print();
    }
    
}
